<template>
  <div>
    <p class="title">用户登录</p>
    <el-form ref="formRef" :model="user" label-width="120px">
      <el-form-item label="登录名称">
        <el-input v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="登录密码">
        <el-input v-model="user.password"></el-input>
      </el-form-item>
    </el-form>
    <el-row justify="end">
      <el-col :span="4" >
        <el-button type="primary" @click="login">登录</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { reactive , getCurrentInstance } from 'vue' ;
import { useRouter } from 'vue-router' ;

const router = useRouter();

const { proxy } = getCurrentInstance();

const user = reactive( { username:'' , password: '' } );

function login(){
    proxy.$http.post( '/user/sign/in' , user ).then( resp => {
        if( resp.status == 200 ) {
            alert( resp.data.message );
            if( resp.data.success ) {
              // 将 JSON Web Token 保存到 sessionStorage 中
              sessionStorage.setItem( 'token' , resp.data.token );
              proxy.$root.logged = true ;
              console.log( proxy.$root.logged );
              // 编程式导航
              router.push( { name: 'bookList' } );
            }
        }
    }).catch( error => {
        console.log( error );
    });
}

</script>

<style>

</style>